﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace INI配置文件的读写
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        fileINI configINI = new fileINI();
        public string ip;
        public string port;
        public string speed;
   
        //声明默认配置文件的路径；
        public string INIpath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "config.ini";
        
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                Task.Run(() =>
                {
                    //fileINI configINI = new fileINI();
                    string section = "机器人参数";
                    ip = textBox1.Text.ToString();
                    port = textBox2.Text.ToString();
                    speed = textBox3.Text.ToString();
                    configINI.INIWrite(section, "IPaddress", ip, INIpath);
                    configINI.INIWrite(section, "Port", port, INIpath);
                    configINI.INIWrite(section, "Speed", speed, INIpath);
                    if (ip !="" && port != ""&& speed != "")
                    {
                        MessageBox.Show("更改成功");
                    }
                    else
                    {
                        MessageBox.Show("请重新输入");
                    }
                });
                
            }
            else
                MessageBox.Show("请先勾选允许更改参数按钮");
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //fileINI configINI = new fileINI();
            string section = "机器人参数";
            textBox1.Text = ip;
            textBox2.Text = port;
            textBox3.Text = speed;
            ip = configINI.INIRead(section, "IPaddress", INIpath);
            port = configINI.INIRead(section, "Port", INIpath);
            speed = configINI.INIRead(section, "Speed", INIpath);

        }

       
        //删除INI参数
        private void button3_Click(object sender, EventArgs e)
        {
            //fileINI configINI = new fileINI();
            if (checkBox2.Checked)
                configINI.INIDelete(INIpath);
            else
                MessageBox.Show("请勾选删除按钮");
        }
    }
}
